package com.my_musc_service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my_musc_service.dao.FavoriteDao;
import com.my_musc_service.entity.Favorite;
import com.my_musc_service.entity.FavoriteVO;
import com.my_musc_service.entity.QueryResult;
import com.my_musc_service.exception.CustomException;
import com.my_musc_service.service.FavoriteService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Favorite)表服务实现类
 *
 * @author makejava
 * @since 2021-06-10 20:44:44
 */
@Service("favoriteService")
public class FavoriteServiceImpl extends ServiceImpl<FavoriteDao, Favorite> implements FavoriteService {
    private final FavoriteDao favoriteDao;

    {
        favoriteDao = getBaseMapper();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Favorite queryById(Integer id) {
        return this.favoriteDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Favorite> queryAllByLimit(int offset, int limit) {
        return this.favoriteDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param favorite 实例对象
     * @return 实例对象
     */
    @Override
    public Favorite insert(Favorite favorite) {
        List<Favorite> favorites = favoriteDao.queryAll(favorite);
        if (favorites.size() > 0) {
            throw new CustomException("当前歌曲已存在");
        }
        this.favoriteDao.insert(favorite);
        return favorite;
    }

    /**
     * 修改数据
     *
     * @param favorite 实例对象
     * @return 实例对象
     */
    @Override
    public Favorite update(Favorite favorite) {
        this.favoriteDao.update(favorite);
        return this.queryById(favorite.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.favoriteDao.deleteById(id) > 0;
    }

    @Override
    public List<Favorite> queryAll(Favorite favorite) {
        return favoriteDao.queryAll(favorite);
    }

    @Override
    public Map<String, Object> selectAllFavSongs(FavoriteVO favoriteVO) {
        Map<String, Object> map = new HashMap<>();
        List<QueryResult> queryResultList = favoriteDao.selectAllFavSongs(favoriteVO);
        if (!CollectionUtils.isEmpty(queryResultList)) {
            queryResultList = queryResultList.stream().map(queryResult -> {
                queryResult.setIsFavorite(true);
                return queryResult;
            }).collect(Collectors.toList());
        }
        Integer rows = favoriteDao.selectAllCountFavSongs(favoriteVO);
        map.put("result", queryResultList);
        map.put("total", rows);
        return map;
    }

    @Override
    public Integer delete(Favorite favorite) {
        return favoriteDao.delete(favorite);
    }
}
